import cv2
import numpy
import os
def show_img(window,img):
    cv2.namedWindow(window,0)
    cv2.resizeWindow(window,480,640)
    cv2.imshow(window,img)

def writePoint(f,point,height,flag,ratio):
    if(flag == 1):
        f.write('PD%d,%d;' % (int((point[0][1])*ratio*40),int(point[0][0]*ratio*40)))
    else:
        f.write('PU%d,%d;' % (int((point[0][1])*ratio*40),int(point[0][0]*ratio*40)))
#ratio:,像素到毫米的换算比例，1像素=ratio毫米
def createPltFile(arr,height,ratio):
    f = open('text.plt','w+')
    f.write('IN;SP1;')
    max_y = 0
    writePoint(f,points_arr[0][0],height,0,ratio)
    max_y = max(points_arr[0][0][0][1],max_y)
    for i in range(len(points_arr)):
        points = points_arr[i]
        first_point = points[0]
        for point in points:
            writePoint(f,point,height,1,ratio)
            max_y = max(point[0][1],max_y)
        writePoint(f,first_point,height,1,ratio)
        max_y = max(first_point[0][1],max_y)
        if i<len(points_arr)-1:
            writePoint(f,points_arr[i+1][0],height,0,ratio)
            max_y = max(points_arr[i+1][0][0][1],max_y)
    writePoint(f,[[0,max_y]],height,0,ratio)
    f.write('PG;@@@@@@@@@@@@@@@@@@@;')

img = cv2.imread("./10638-35x35.jpg")
img = cv2.bitwise_not(img)
#转为灰度图
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

kernel = numpy.ones((3, 3), numpy.uint8)
img_dilate = cv2.dilate(gray, kernel,iterations = 3)
img_dilate = cv2.erode(gray, kernel,iterations = 3)

#gray = cv2.Canny(gray, 100, 300)
#图像二值化
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY| cv2.THRESH_OTSU)
#得到轮廓
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_TC89_L1)
draw_img_1 = numpy.ones(img.shape,dtype=numpy.uint8)
index = 0
points_arr = []
for i in range(len(hierarchy[0])-1,-1,-1):
    item = hierarchy[0][i]
    if item[3] == -1:
        cv2.drawContours(draw_img_1,contours,i,(0,0,255),3)
        points_arr.append(contours[i])
    else:
        father_id = item[3]
        l1 = cv2.arcLength(contours[father_id],True)
        l2 = cv2.arcLength(contours[i],True)
        if l2/l1<0.8:
            cv2.drawContours(draw_img_1,contours,i,(0,0,255),3)
            points_arr.append(contours[i])

#cv2.drawContours(draw_img_1,contours,-1,(0,0,255),1)

show_img('input',img)
show_img('gray',gray)
show_img('thresh',thresh)
show_img('contours',draw_img_1)
cv2.imwrite('contours.png',draw_img_1)

#绘制线条加工轨迹到draw_img_2中
draw_img_2 = numpy.zeros(img.shape,dtype=numpy.uint8)
for i in range(len(points_arr)):
    points = points_arr[i]
    old_point = points[0]
    for point in points:
        cv2.line(draw_img_2,(old_point[0][0],old_point[0][1]),(point[0][0],point[0][1]),(0,255,0),3)
        old_point = point
    cv2.line(draw_img_2,(old_point[0][0],old_point[0][1]),(points[0][0][0],points[0][0][1]),(0,255,0),3)
    if i<len(points_arr)-1:
        cv2.line(draw_img_2,(points_arr[i][0][0][0],points_arr[i][0][0][1]),(points_arr[i+1][0][0][0],points_arr[i+1][0][0][1]),(255,0,0),3)
show_img('animation',draw_img_2)
cv2.imwrite('contours2.png',draw_img_2)
createPltFile(points_arr,img.shape[0],1.0/10)

cv2.waitKey(0)